package com.mushuilingfeng.ddd.user.infrastructure.repository;

import com.mushuilingfeng.ddd.user.domain.base.BusinessException;
import com.mushuilingfeng.ddd.user.domain.base.Ii8nCode;
import com.mushuilingfeng.ddd.user.domain.user.repository.IUserLoginLogRepository;
import com.mushuilingfeng.ddd.user.domain.user.vo.UserLoginLog;
import com.mushuilingfeng.ddd.user.infrastructure.cover.IPODataCover;
import com.mushuilingfeng.ddd.user.infrastructure.repository.dao.mapperservice.UserLoginLogMapperService;
import com.mushuilingfeng.ddd.user.infrastructure.repository.po.UserLoginLogPO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.Date;

/**
 * @author 沐水淋风
 * @date 2024/10/5
 */
@Repository
@AllArgsConstructor
public class UserLoginLogRepositoryImpl extends BaseMybatisRepository<UserLoginLog> implements IUserLoginLogRepository {
    private final UserLoginLogMapperService mapperService;
    
    @Override
    public UserLoginLog get(Serializable id) {
        UserLoginLogPO po = mapperService.getById(id);
        if (po == null) {
            return null;
        }
        return IPODataCover.INSTANCE.userLoginLogPO2UserLoginLog(po);
    }
    
    @Override
    public void save(UserLoginLog entityOrObjectValue) {
        UserLoginLogPO po = IPODataCover.INSTANCE.userLoginLog2UserLoginLogPO(entityOrObjectValue);
        if (null == po.getCreateTime()) {
            po.setCreateTime(new Date());
        }
        po.setUpdateTime(new Date());
        mapperService.saveOrUpdate(po);
    }
    
    @Override
    public void saveOrUpdate(UserLoginLog entityOrObjectValue) {
        throw new BusinessException(Ii8nCode.DATA_NOT_ALLOWED_TO_UPDATE);
    }
    
    @Override
    public void delete(Serializable id) {
        // 日志数据不允许删除
        throw new BusinessException(Ii8nCode.DATA_NOT_ALLOWED_TO_DELETE);
    }
}
